package slu.appli.wodtimer.swing.component.text;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import slu.appli.wodtimer.swing.component.keypanel.XKeyPanel;
import slu.appli.wodtimer.swing.cst.Text4ComponentCst;

public class XRequestManager {
	/**
	 * 
	 */
	private static final String IMG_BTN_MENU = "slu/ressources/icon/btn/misc.png";
	private static final String IMG_BTN_GRP = "slu/ressources/icon/btn/reload.png";
	private static final String IMG_BTN_DEL = "slu/ressources/icon/btn/editdelete.png";
	private static final String IMG_BTN_WIZ = "slu/ressources/icon/btn/wizard.png";
	private static final String IMG_BTN_CLEAR = "slu/ressources/icon/btn/clear.png";
	
	private static final ImageIcon ICON_BTN_MENU = 
			new ImageIcon(XKeyPanel.class.getClassLoader().getResource(IMG_BTN_MENU));
	private static final ImageIcon ICON_BTN_GRP = 
			new ImageIcon(XKeyPanel.class.getClassLoader().getResource(IMG_BTN_GRP));
	private static final ImageIcon ICON_BTN_DEL = 
			new ImageIcon(XKeyPanel.class.getClassLoader().getResource(IMG_BTN_DEL));
	private static final ImageIcon ICON_BTN_WIZ = 
			new ImageIcon(XKeyPanel.class.getClassLoader().getResource(IMG_BTN_WIZ));
	private static final ImageIcon ICON_BTN_CLEAR = 
			new ImageIcon(XKeyPanel.class.getClassLoader().getResource(IMG_BTN_CLEAR));

	private static final String[] STR_EXAMPLES = 
			{"10;2;1","1;1;1","7(20;10);20","5m;4(1m;1m30s;1m;1m30s"};

	public static final String ACT_REQT_DEL = "del";
	public static final String ACT_REQT_ADD_GRP = "grp";
	public static final String ACT_REQT_WISARD = "wiz";
	public static final String ACT_REQT_CLEAR = "clear";
	
	private final JLabel menu;

	public XRequestManager(ActionListener listener) {
		super();
		JPopupMenu popupMenu = new JPopupMenu();
		this.menu = new JLabel(ICON_BTN_MENU);
		this.menu.setComponentPopupMenu(popupMenu);
		this.menu.setToolTipText(Text4ComponentCst.TIP_POPUP);
		JMenuItem item = null;
		JMenu m = new JMenu(Text4ComponentCst.MNU_CYCLE);
		m.setIcon(ICON_BTN_GRP);
		popupMenu.add(m);
		for (int i=2; i<10;i++) {
			item = new JMenuItem(ICON_BTN_GRP);
			item.setActionCommand(ACT_REQT_ADD_GRP);
			item.setText(i + "(t)");
			item.addActionListener(listener);
			m.add(item);
		}
		m = new JMenu(Text4ComponentCst.MNU_INSERT_WOD);
		m.setIcon(ICON_BTN_WIZ);
		popupMenu.add(m);
		for (int i=0; i<STR_EXAMPLES.length;i++) {
			item = new JMenuItem(ICON_BTN_WIZ);
			item.setActionCommand(ACT_REQT_WISARD);
			item.setText(STR_EXAMPLES[i]);
			item.addActionListener(listener);
			m.add(item);
		}
		popupMenu.add(new JSeparator());
		item = new JMenuItem(Text4ComponentCst.MNU_DELETE, ICON_BTN_DEL);
		item.setActionCommand(ACT_REQT_DEL);
		item.addActionListener(listener);
		popupMenu.add(item);
		item = new JMenuItem(Text4ComponentCst.MNU_CLEAR, ICON_BTN_CLEAR);
		item.setActionCommand(ACT_REQT_CLEAR);
		item.addActionListener(listener);
		popupMenu.add(item);
	}
	
	public JComponent getComponent() {
		return this.menu;
	}
}
